package com.gemme.gemme.wode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.login.MsgBean;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.util.DpUtil;
import com.gemme.gemme.util.ShapeUtil;
import com.gemme.gemme.util.asrsa.RsaNewUtils;
import com.gemme.gemme.view.Topview;
import com.jaeger.library.StatusBarUtil;

import java.security.PublicKey;
import java.util.HashMap;

//侧滑删除地址
public class AddAnquanLingpaiActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anquanlingpai);
        initTop();
        initView();
    }

    EditText et_acount;
    EditText et_msg;
    TextView tv_code;
    TextView tv_submit;
    private void initView() {
        et_acount = findViewById(R.id.et_acount);
        et_msg = findViewById(R.id.et_msg);
        tv_code = findViewById(R.id.tv_code);
        tv_submit = findViewById(R.id.tv_submit);

        tv_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = et_acount.getText() + "";
                if(TextUtils.isEmpty(account)){
                    Toast.makeText(mContext, "安全令牌不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                toMsg();
                tv_code.setEnabled(false);
                tv_code.setBackground(ShapeUtil.setCornBgStrok(DpUtil.dp2px((Activity) mContext,4),"#ffffffff",
                        DpUtil.dp2px((Activity) mContext,1),"#FF9D2C2F"));
                initTimer();
            }
        });


        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                toServer();
                if(TextUtils.isEmpty(msg)){
                    Toast.makeText(mContext, "验证码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                String aqlp = et_acount.getText() + "";
                if(TextUtils.isEmpty(aqlp)){
                    Toast.makeText(mContext, "安全令牌不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent i = new Intent();
                i.putExtra("aqlp", aqlp);
                i.putExtra("msg", msg);
                setResult(3, i);
                finish();
            }
        });
    }

    private void toServer() {
        if(TextUtils.isEmpty(msg)){
            Toast.makeText(mContext, "验证码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String aqlp = et_acount.getText() + "";
        if(TextUtils.isEmpty(aqlp)){
            Toast.makeText(mContext, "安全令牌不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String rsa = testRsa(aqlp);
       if(TextUtils.isEmpty(rsa))
           return;
        String url = DataManager.BaseUrl + "user/userapp/editexpassword?" +
                "code=" + msg +
                "&exPassword=" + rsa;
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(this, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                   if(DataPares.isSucess(mContext,response)){
                       /*Toast.makeText(mContext, "修改安全令牌成功", Toast.LENGTH_SHORT).show();
                       Intent i = new Intent();
                       i.putExtra("aqlp", aqlp);
                       i.putExtra("msg", msg);
                       setResult(3, i);*/
                   }else{
                       Toast.makeText(mContext, "修改安全令牌失败", Toast.LENGTH_SHORT).show();
                   }
                    System.out.println("");
                    finish();
                }catch (Exception e){

                }
            }
        });
    }


    String msg ;
    private void toMsg() {
        String account = et_acount.getText() + "";
        if(TextUtils.isEmpty(account)){
            Toast.makeText(mContext, "安全令牌不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

//        String url = "http://192.168.1.6:9999/user/userapp/appmobile/15256987478";
        String url = DataManager.BaseUrl + "user/userapp/appexpassword?smsType=" + "withdrawal";
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(this, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    System.out.println("");
                    MsgBean msgBean = JSON.parseObject(response, MsgBean.class);

                    msg = msgBean.getMsg();
                    et_msg.setText(msg);

//


                }catch (Exception e){

                }
            }
        });
    }

    private void initTimer() {
        /**
         * CountDownTimer timer = new CountDownTimer(3000, 1000)中，
         * 第一个参数表示总时间，第二个参数表示间隔时间。
         * 意思就是每隔一秒会回调一次方法onTick，然后1秒之后会回调onFinish方法。
         */
        CountDownTimer timer = new CountDownTimer(60000, 1000) {
            public void onTick(long millisUntilFinished) {
                Log.d("TelLoginActivity","倒计时" + millisUntilFinished / 1000 + "秒");
                tv_code.setText(millisUntilFinished / 1000 + "秒");
                if(millisUntilFinished / 1000 == 0){
                    tv_code.setEnabled(true);
                    tv_code.setBackgroundResource(0);
                    tv_code.setText("获取验证码");
                }

            }

            public void onFinish() {

            }
        };
        //调用 CountDownTimer 对象的 start() 方法开始倒计时，也不涉及到线程处理
        timer.start();
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.getContentText().setVisibility(View.GONE);
    }



    private String testRsa(String aqlp) {
        String str_rsa = "";
        try {
            if(TextUtils.isEmpty(aqlp))
                return str_rsa;
            PublicKey publicKey = RsaNewUtils.loadPublicKey(Constant.PUBLICKEY);
//            PrivateKey privateKey = RsaNewUtils.loadPrivateKey(Constant.privateKey);
//            String str = "hello";
            String publicRsa = RsaNewUtils.encryptDataStr(aqlp.getBytes("utf-8"), publicKey);
            str_rsa = publicRsa;
//            byte[] bytes = RsaNewUtils.decryptData(publicRsa.getBytes("utf-8"), privateKey);
//            String privateRsa = new String(bytes);
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return str_rsa;
    }


}